package com.dayuan.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayuan.bean.UserInfo;
import com.dayuan.constant.AjaxMsg;
import com.dayuan.service.ShopCartService;
import com.dayuan.vo.ResultVo;
import com.dayuan.vo.ShopCartVo;

@Controller
@RequestMapping("/shopcart")
public class ShopCartController {

	@Resource
	private ShopCartService shopCartService;

	private static Logger logger = LoggerFactory.getLogger(ShopCartController.class);

	/* 根据用户id查询购物车所有商品 */
	@RequestMapping(value = "/selectshopcartbyuserid.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo selectShopCartByUserId(HttpSession session) {
		ResultVo resultVo = new ResultVo();
		try {
			UserInfo userinfo = (UserInfo) session.getAttribute("userinfo");
			List<ShopCartVo> list = shopCartService.selectShopCartByUserId(userinfo.getId());
			/*if (list.size() == 0) {
				resultVo.setCode(AjaxMsg.FAIL.getValue());
				resultVo.setMsg("该用户购物车没有商品");
				return resultVo;
			}*/
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			resultVo.setData(list);
			return resultVo;
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网络错误，请稍后再试");
			return resultVo;

		}

	}

	/* 根据购物车id删除商品 */
	@RequestMapping(value = "/deleteshopcart.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo selectShopCartByUserId(Integer id) {
		ResultVo resultVo = new ResultVo();
		try {
			int temp = shopCartService.deleteShopCart(id);
			if (temp == 0) {
				resultVo.setCode(AjaxMsg.FAIL.getValue());
				resultVo.setMsg("删除物品不存在");
				return resultVo;
			}
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			resultVo.setMsg("删除成功");
			return resultVo;
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网络错误，请稍后再试");
			return resultVo;

		}

	}

	/* 商品加入购物车 */
	@RequestMapping(value = "/insertshopcart.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo insertShopCart(HttpSession session, Integer goodsID, Integer number) {
		ResultVo resultVo = new ResultVo();
		try {
			UserInfo userinfo = (UserInfo) session.getAttribute("userinfo");
			shopCartService.insertShopCart(goodsID, userinfo.getId(), number);
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			resultVo.setMsg("加入商品成功");
			return resultVo;
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网络错误，请稍后再试");
			return resultVo;

		}

	}
	
	
	/*修改物品数量*/
	@RequestMapping(value = "/updateshopcartinfo.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo updateShopCartInfo(Integer id, Integer number) {
		ResultVo resultVo = new ResultVo();
		try {

			int temp = shopCartService.updateShopCartInfo(id, number);
			if (temp == 0) {
				resultVo.setCode(AjaxMsg.FAIL.getValue());
				resultVo.setMsg("更改错误");
				return resultVo;
			}
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			resultVo.setMsg("更改数量成功");
			return resultVo;

		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网络错误，请稍后再试");
			return resultVo;
		}

	}

}
